<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="交易id" prop="transactionId">
        <el-input
          v-model="queryParams.transactionId"
          placeholder="请输入交易id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="外部订单号" prop="outTradeNo">
        <el-input
          v-model="queryParams.outTradeNo"
          placeholder="请输入外部订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

       <el-form-item label="订单状态" prop="status">
         <el-select v-model="queryParams.status" placeholder="请选择" clearable>
           <el-option
             v-for="dict in dict.type.order_status"
             :key="dict.value"
             :label="dict.label"
             :value="dict.value"
           />
         </el-select>
      </el-form-item>

      <el-form-item label="支付方式" prop="type">
         <el-select v-model="queryParams.type" placeholder="请选择" clearable>
           <el-option
             v-for="dict in dict.type.pay_way"
             :key="dict.value"
             :label="dict.label"
             :value="dict.value"
           />
         </el-select>
      </el-form-item>




      <el-form-item label="创建时间">
            <el-date-picker size="small" v-model="uploadTime" @change="handleQuery" unlink-panels :picker-options="pickerOptions" 　 style="width: 220px" value-format="yyyy-MM-dd" type="daterange"
                            range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
    <!--
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['course:goodsOrderPays:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['course:goodsOrderPays:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['course:goodsOrderPays:remove']"
        >删除</el-button>
      </el-col>
      -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['course:goodsOrderPays:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsOrderPaysList" @selection-change="handleSelectionChange">

     <el-table-column label="外部订单号" align="center" prop="outTradeNo" min-width="150" />
      <el-table-column label="交易编号" align="center" prop="transactionId" />

      <el-table-column label="支付方式" align="center" prop="type" >
        <template slot-scope="scope">
           <dict-tag :options="dict.type.pay_way" :value="scope.row.type"/>
         </template>
       </el-table-column>
      <el-table-column label="总价" align="center" prop="totalMoney" />
      <el-table-column label="支付金额" align="center" prop="payMoney" />
      <el-table-column label="支付积分" align="center" prop="payPoint" />
      <el-table-column label="积分兑换率" align="center" prop="pointRate" />
      <el-table-column label="状态" align="center" prop="status" >
       <template slot-scope="scope">
           <dict-tag :options="dict.type.goods_order_pays_status" :value="scope.row.status"/>
         </template>
       </el-table-column>
       <el-table-column label="创建时间" align="center" prop="createTime" min-width="180"/>
       <el-table-column label="更新时间" align="center" prop="updateTime" min-width="180"/>

       <!--
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['course:goodsOrderPays:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['course:goodsOrderPays:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
      -->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品支付对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单ID" prop="goodsOrderId">
          <el-input v-model="form.goodsOrderId" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="交易id" prop="transactionId">
          <el-input v-model="form.transactionId" placeholder="请输入交易id" />
        </el-form-item>
        <el-form-item label="外部订单号" prop="outTradeNo">
          <el-input v-model="form.outTradeNo" placeholder="请输入外部订单号" />
        </el-form-item>
        <el-form-item label="总价" prop="totalMoney">
          <el-input v-model="form.totalMoney" placeholder="请输入总价" />
        </el-form-item>
        <el-form-item label="支付金额" prop="payMoney">
          <el-input v-model="form.payMoney" placeholder="请输入支付金额" />
        </el-form-item>
        <el-form-item label="支付积分" prop="payPoint">
          <el-input v-model="form.payPoint" placeholder="请输入支付积分" />
        </el-form-item>
        <el-form-item label="积分兑换率" prop="pointRate">
          <el-input v-model="form.pointRate" placeholder="请输入积分兑换率" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoodsOrderPays, getGoodsOrderPays, delGoodsOrderPays, addGoodsOrderPays, updateGoodsOrderPays } from "@/api/course/goodsOrderPays";

export default {
  name: "GoodsOrderPays",
  dicts: ['goods_order_pays_status','pay_way'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品支付表格数据
      goodsOrderPaysList: [],
      uploadTime: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsOrderId: null,
        transactionId: null,
        outTradeNo: null,
        type: null,
        totalMoney: null,
        payMoney: null,
        payPoint: null,
        pointRate: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsOrderId: [
          { required: true, message: "订单ID不能为空", trigger: "blur" }
        ],
        transactionId: [
          { required: true, message: "交易id不能为空", trigger: "blur" }
        ],
        outTradeNo: [
          { required: true, message: "外部订单号不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "支付方式：1微信不能为空", trigger: "change" }
        ],
        totalMoney: [
          { required: true, message: "总价不能为空", trigger: "blur" }
        ],
        payMoney: [
          { required: true, message: "支付金额不能为空", trigger: "blur" }
        ],
        payPoint: [
          { required: true, message: "支付积分不能为空", trigger: "blur" }
        ],
        pointRate: [
          { required: true, message: "积分兑换率不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态：1待支付，2支付成功，3支付失败，4已取消不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.uploadTime[0] = this.getNearDay(-30);
    this.uploadTime[1] = this.getNearDay(0);
    this.getList();
  },
  methods: {
    /** 查询商品支付列表 */
    getList() {
      this.loading = true;
     if (this.uploadTime) {
          this.queryParams.beginDate = this.uploadTime[0];
          this.queryParams.endDate = this.uploadTime[1];
      } else {
          this.queryParams.beginDate = '';
          this.queryParams.endDate = '';
      }

      listGoodsOrderPays(this.queryParams).then(response => {
        this.goodsOrderPaysList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsOrderId: null,
        transactionId: null,
        outTradeNo: null,
        type: null,
        totalMoney: null,
        payMoney: null,
        payPoint: null,
        pointRate: null,
        status: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品支付";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoodsOrderPays(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品支付";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoodsOrderPays(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoodsOrderPays(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品支付编号为"' + ids + '"的数据项？').then(function() {
        return delGoodsOrderPays(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('course/goodsOrderPays/export', {
        ...this.queryParams
      }, `goodsOrderPays_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
